home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n02 / strucprg.asc < prev    next >
Encoding:
Text File  |  1991-12-17  |  7.0 KB  |  198 lines

  1. _STRUCTURED PROGRAMMING COLUMN_
  2. by Jeff Duntemann
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. {---------------------------------}
  8. {   METHODS: TMortgageTopInterior }
  9. {---------------------------------}
  10.  
  11. CONSTRUCTOR TMortgageTopInterior.Init(VAR Bounds : TRect);
  12.  
  13. BEGIN
  14.   TView.Init(Bounds);     { Call ancestor's constructor }
  15.   GrowMode := gfGrowHiX;  { Permits pane to grow in X but not Y }
  16. END;
  17.  
  18. PROCEDURE TMortgageTopInterior.Draw;
  19. VAR
  20.   YRun  : Integer;
  21.   Color : Byte;
  22.   B     : TDrawBuffer;
  23.   STemp : String[20];
  24. BEGIN
  25.   Color := GetColor(1);
  26.   MoveChar(B,' ',Color,Size.X);    { Clear the buffer to spaces }
  27.   MoveStr(B,'  Principal    Interest   Periods',Color);
  28.   WriteLine(0,0,Size.X,1,B);
  29.  
  30.   MoveChar(B,' ',Color,Size.X);    { Clear the buffer to spaces }
  31.   { Here we convert payment data to strings for display: }
  32.   Str(Mortgage^.Principal:7:2,STemp);
  33.   MoveStr(B[2],STemp,Color);         { At beginning of buffer B }
  34.   Str(Mortgage^.Interest*100:7:2,STemp);
  35.   MoveStr(B[14],STemp,Color);      { At position 14 of buffer B }
  36.   Str(Mortgage^.Periods:4,STemp);
  37.   MoveStr(B[27],STemp,Color);      { At position 27 of buffer B }
  38.   WriteLine(0,1,Size.X,1,B);
  39.  
  40.   MoveChar(B,' ',Color,Size.X);    { Clear the buffer to spaces }
  41.   MoveStr(B,
  42.   '                                      Extra        Principal      Interest',
  43.   Color);
  44.   WriteLine(0,2,Size.X,1,B);
  45.  
  46.   MoveChar(B,' ',Color,Size.X);    { Clear the buffer to spaces }
  47.   MoveStr(B,
  48.   'Paymt #  Prin.   Int.     Balance     Principal    So far         So far ',
  49.   Color);
  50.   WriteLine(0,3,Size.X,1,B);
  51.  
  52. END;
  53.  
  54. {------------------------------------}
  55. {   METHODS: TMortgageBottomInterior }
  56. {------------------------------------}
  57.  
  58. CONSTRUCTOR TMortgageBottomInterior.Init(VAR Bounds : TRect;
  59.                                        AHScrollBar, AVScrollBar : PScrollBar);
  60. BEGIN
  61.   { Call ancestor's constructor: }
  62.   TScroller.Init(Bounds,AHScrollBar,AVScrollBar);
  63.   GrowMode := gfGrowHiX + gfGrowHiY;
  64.   Options := Options OR ofFramed;
  65. END;
  66.  
  67. PROCEDURE TMortgageBottomInterior.Draw;
  68. VAR
  69.   Color : Byte;
  70.   B     : TDrawBuffer;
  71.   YRun  : Integer;
  72.   STemp : String[20];
  73. BEGIN
  74.   Color := GetColor(1);
  75.   FOR YRun := 0 TO Size.Y-1 DO
  76.     BEGIN
  77.       MoveChar(B,' ',Color,80);    { Clear the buffer to spaces }
  78.       Str(Delta.Y+YRun+1:4,STemp);
  79.       MoveStr(B,STemp+':',Color);        { At beginning of buffer B }
  80.       { Here we convert payment data to strings for display: }
  81.       Str(Mortgage^.Payments^[Delta.Y+YRun+1].PayPrincipal:7:2,STemp);
  82.       MoveStr(B[6],STemp,Color);         { At beginning of buffer B }
  83.       Str(Mortgage^.Payments^[Delta.Y+YRun+1].PayInterest:7:2,STemp);
  84.       MoveStr(B[15],STemp,Color);      { At position 15 of buffer B }
  85.       Str(Mortgage^.Payments^[Delta.Y+YRun+1].Balance:10:2,STemp);
  86.       MoveStr(B[24],STemp,Color);      { At position 24 of buffer B }
  87.       { There isn't an extra principal value for every payment, so }
  88.       { display the value only if it is nonzero:                   }
  89.       STemp := '';
  90.       IF  Mortgage^.Payments^[Delta.Y+YRun+1].ExtraPrincipal > 0
  91.       THEN
  92.         Str(Mortgage^.Payments^[Delta.Y+YRun+1].ExtraPrincipal:10:2,STemp);
  93.       MoveStr(B[37],STemp,Color);      { At position 37 of buffer B }
  94.       Str(Mortgage^.Payments^[Delta.Y+YRun+1].PrincipalSoFar:10:2,STemp);
  95.       MoveStr(B[50],STemp,Color);      { At position 50 of buffer B }
  96.       Str(Mortgage^.Payments^[Delta.Y+YRun+1].InterestSoFar:10:2,STemp);
  97.       MoveStr(B[64],STemp,Color);      { At position 64 of buffer B }
  98.       { Here we write the line to the window, taking into account the }
  99.       { state of the X scroll bar: }
  100.       WriteLine(0,YRun,Size.X,1,B[Delta.X]);
  101.     END;
  102. END;
  103.  
  104. {------------------------------}
  105. {   METHODS: TMortgageView     }
  106. {------------------------------}
  107.  
  108. CONSTRUCTOR TMortgageView.Init(VAR Bounds  : TRect;
  109.                                    ATitle  : TTitleStr;
  110.                                    ANumber : Integer;
  111.                                    InitMortgageData :
  112.                                    MortgageDialogData);
  113. VAR
  114.   TopInterior    : PMortgageTopInterior;
  115.   BottomInterior : PMortgageBottomInterior;
  116.   HScrollBar,VScrollBar : PScrollBar;
  117.   R,S  : TRect;
  118. BEGIN
  119.   TWindow.Init(Bounds,ATitle,ANumber); { Call ancestor's constructor }
  120.   { Call the Mortgage object's constructor using dialog data: }
  121.   WITH InitMortgageData DO
  122.     Mortgage.Init(PrincipalData,
  123.                   InterestData / 100,
  124.                   PeriodsData,
  125.                   12);
  126.   { Here we set up a window with *two* interiors, one scrollable, one }
  127.   { static.  It's all in the way that you define the bounds, mostly:  }
  128.   GetClipRect(Bounds);             { Get bounds for interior of view  }
  129.   Bounds.Grow(-1,-1);      { Shrink those bounds by 1 for both X & Y  }
  130.  
  131.   { Define a rectangle to embrace the upper of the two interiors:     }
  132.   R.Assign(Bounds.A.X,Bounds.A.Y,Bounds.B.X,Bounds.A.Y+4);
  133.   TopInterior := New(PMortgageTopInterior,Init(R));
  134.   TopInterior^.Mortgage := @Mortgage;
  135.   Insert(TopInterior);
  136.  
  137.   { Define a rectangle to embrace the lower of two interiors: }
  138.   R.Assign(Bounds.A.X,Bounds.A.Y+5,Bounds.B.X,Bounds.B.Y);
  139.  
  140.   { Create scroll bars for both mouse & keyboard input: }
  141.   VScrollBar := StandardScrollBar(sbVertical + sbHandleKeyboard);
  142.   { We have to adjust vertical bar to fit bottom interior: }
  143.   VScrollBar^.Origin.Y := R.A.Y;       { Adjust top Y value }
  144.   VScrollBar^.Size.Y := R.B.Y - R.A.Y; { Adjust size }
  145.   { The horizontal scroll bar, on the other hand, is standard: }
  146.   HScrollBar := StandardScrollBar(sbHorizontal + sbHandleKeyboard);
  147.  
  148.   { Create bottom interior object with scroll bars: }
  149.   BottomInterior :=
  150.     New(PMortgageBottomInterior,Init(R,HScrollBar,VScrollBar));
  151.   { Make copy of pointer to mortgage object: }
  152.   BottomInterior^.Mortgage := @Mortgage;
  153.   { Set the limits for the scroll bars: }
  154.   BottomInterior^.SetLimit(80,InitMortgageData.PeriodsData);
  155.   { Insert the interior into the window: }
  156.   Insert(BottomInterior);
  157. END;
  158.  
  159.  
  160.  
  161.  
  162. [LISTING TWO]
  163.  
  164.   PMortgageTopInterior = ^TMortgageTopInterior;
  165.   TMortgageTopInterior =
  166.     OBJECT(TView)
  167.       Mortgage    : PMortgage;
  168.       CONSTRUCTOR Init(VAR Bounds : TRect);
  169.       PROCEDURE   Draw; VIRTUAL;
  170.     END;
  171.  
  172.   PMortgageBottomInterior = ^TMortgageBottomInterior;
  173.   TMortgageBottomInterior =
  174.     OBJECT(TScroller)
  175.       { Points to Mortgage object owned by TMortgageView }
  176.       Mortgage    : PMortgage;
  177.       CONSTRUCTOR Init(VAR Bounds : TRect;
  178.                        AHScrollBar, AVScrollbar : PScrollBar);
  179.       PROCEDURE   Draw; VIRTUAL;
  180.     END;
  181.  
  182.   PMortgageView = ^TMortgageView;
  183.   TMortgageView =
  184.     OBJECT(TWindow)
  185.       Mortgage    : TMortgage;
  186.       CONSTRUCTOR Init(VAR Bounds  : TRect;
  187.                        ATitle  : TTitleStr;
  188.                        ANumber : Integer;
  189.                        InitMortgageData :
  190.                        MortgageDialogData);
  191.       PROCEDURE   HandleEvent(Var Event : TEvent); VIRTUAL;
  192.       PROCEDURE   ExtraPrincipal;
  193.       PROCEDURE   PrintSummary;
  194.       DESTRUCTOR  Done; VIRTUAL;
  195.     END;
  196.  
  197.  
  198.